package com.moweilin.hello_server;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.FileUpload;
import io.vertx.ext.web.RoutingContext;

import java.io.File;
import java.util.Set;

/**
 * Created by moweilin on 2016/4/25.
 */
public class FormHandler implements Handler<RoutingContext>{
    @Override
    public void handle(RoutingContext event) {

        switch (event.request().method()){
            case GET:
                get(event,event.request(),event.response());
                break;
            case POST:
                post(event,event.request(),event.response());
                break;
            case PUT:
                break;
            case DELETE:
                break;
            case PATCH:
                break;
            default:
                event.fail(HttpResponseStatus.METHOD_NOT_ALLOWED.code());
        }
    }
    protected void get(RoutingContext routingContext, HttpServerRequest request, HttpServerResponse response){
        HtmlWriter htmlWriter = HtmlWriter.create(request,response);
        htmlWriter.startHtml();
        htmlWriter.writeRequestHeader();
        htmlWriter.writeResponseHeader();

        response.write("<h3>form</h3>");
        String path  = routingContext.request().path();
        String contextPath = request.getHeader("Context-Path");
        path = "/" + contextPath + path;
        response.write("<form action=\""+ path + "\" method=\"post\"  enctype=\"multipart/form-data\">");
        response.write("<p>First name: <input type=\"text\" name=\"fname\" /></p>");
        response.write("<p>Last name: <input type=\"text\" name=\"lname\" /></p>");
        response.write("<p>File: <input type=\"file\" name=\"ufile\" /></p>");
        response.write(" <input type=\"submit\" value=\"Submit\" />");
        response.write("</form>");
        htmlWriter.endHtml();
        response.end();
    }

    protected void post(RoutingContext routingContext, HttpServerRequest request, HttpServerResponse response){
        HtmlWriter htmlWriter = HtmlWriter.create(request,response);
        htmlWriter.startHtml();
        htmlWriter.writeRequestHeader();
        htmlWriter.writeTitle("submit form");
//        htmlWriter.pump();
        MultiMap multiMap = routingContext.request().params();
        multiMap.forEach(entry -> {
            htmlWriter.writeField(entry.getKey(),entry.getValue());
        });
        htmlWriter.writeTitle("upload");
        Set<FileUpload> fileUploadSet = routingContext.fileUploads();
        fileUploadSet.forEach(fileUpload -> {
            htmlWriter.writeField(fileUpload.uploadedFileName(),fileUpload.fileName());
        });
        htmlWriter.endHtml();
        htmlWriter.end();
    }

    protected void put(RoutingContext routingContext, HttpServerRequest request, HttpServerResponse response){
        routingContext.fail(HttpResponseStatus.METHOD_NOT_ALLOWED.code());
    }

    protected void delete(RoutingContext routingContext, HttpServerRequest request, HttpServerResponse response){
        routingContext.fail(HttpResponseStatus.METHOD_NOT_ALLOWED.code());
    }

    protected void path(RoutingContext routingContext, HttpServerRequest request, HttpServerResponse response){
        routingContext.fail(HttpResponseStatus.METHOD_NOT_ALLOWED.code());
    }

}
